/**
 * COPYRIGHT (C) 2009. Denny Tran, Omar Jaime, David Deng, Julie Hsieh. All Rights Reserved
 * A panel to hold the calendar and its functions.  
 * @author Denny Tran, Omar Jaime, David Deng, Julie Hsieh 
 * @version 1.01 2009-05-05
 */

package gui;
import java.awt.*;
import java.util.ArrayList;
import java.util.GregorianCalendar;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import data.Event;
import data.Group;
import data.MyCalendar;
import data.User;

public class CalendarPanel extends ViewPanel
{
    private JTabbedPane tabPanel;
    private GregorianCalendar current;
    private ArrayList<CalendarComponent> calendarComponents;
    private JScrollPane calendarView;
    private ViewManager viewManager;

    /**
     * Constructs a panel with a calendar and calendar functions.
     * @param viewManager The viewManager to attach to this panel.
     * @param identification An identification string to identify this panel.
     */
    public CalendarPanel(ViewManager viewManager, String identification)
    {
        super(viewManager, identification);
        this.viewManager = viewManager;
        calendarComponents = new ArrayList<CalendarComponent>();

        current = new GregorianCalendar();
        // display calendar
        tabPanel = new JTabbedPane();
        tabPanel.setLocation(0,0);
        tabPanel.setSize(700,480);
        
        Group aGroup = viewManager.getGs().getGroup();
        ArrayList<User> users = aGroup.getUsers();
        
        // project's component
        CalendarComponent comp = new CalendarComponent(viewManager, this);
        calendarComponents.add(comp);
        tabPanel.addTab("Project", null, comp, "Project");
        
        // user's component
        for (User u : users)
        {            
            comp = new CalendarComponent(viewManager, this);
            calendarComponents.add(comp);            
            tabPanel.addTab(u.getRealName(), null, comp, u.getRealName());
        }
        
        // all's component
        comp = new CalendarComponent(viewManager, this);
        calendarComponents.add(comp);
        tabPanel.addTab("All", null, comp, "All");
        
        this.add(tabPanel);
        tabPanel.addChangeListener(new ChangeListener()
        {

            @Override
            public void stateChanged(ChangeEvent arg0)
            {
                reload();
            }
            
        });
        
        this.reload(); // initialize first tab
        
        // buttons
        int buttonX = 50;
        int buttonY = 500;
        int buttonSpread = 200;
        Dimension buttonSize = new Dimension(150,20);
        
        JButton addEventButton = new JButton("Add Event");
        addEventButton.setLocation(buttonX, buttonY);
        buttonX += buttonSpread;
        addEventButton.setSize(buttonSize);
        addEventButton.addActionListener(viewManager.addLink("add event"));
        this.add(addEventButton);
        
        JButton addDeadlineButton = new JButton("Add Deadline");
        addDeadlineButton.setLocation(buttonX, buttonY);
        buttonX += buttonSpread;
        addDeadlineButton.setSize(buttonSize);
        addDeadlineButton.addActionListener(viewManager.addLink("add deadline"));
        this.add(addDeadlineButton);
        
        JButton addMeetingButton = new JButton("Add Meeting");
        addMeetingButton.setLocation(buttonX, buttonY);
        buttonX += buttonSpread;
        addMeetingButton.setSize(buttonSize);
        addMeetingButton.addActionListener(viewManager.addLink("add meeting"));
        this.add(addMeetingButton);
        
        if (!viewManager.getGs().getUser().isAdmin())
            addMeetingButton.setVisible(false);
    }

    /**
     * Reloads the panel with the events of the date that it was set to.
     */
    public void reload()
    {
        int index = tabPanel.getSelectedIndex();
        ArrayList<Event> list = new ArrayList<Event>();
        
        if (index == 0) // project
        {
            list = viewManager.getGs().getGroup().getGroupCalendar().getWeekEvents(current);
        }
        else if (index == tabPanel.getTabCount() - 1) // all tab
        {
            list.addAll(viewManager.getGs().getGroup().getGroupCalendar().getWeekEvents(current));
            ArrayList<User> users = viewManager.getGs().getGroup().getUsers();
            for (User u : users)
                list.addAll(u.getUserCalendar().getWeekEvents(current));
        }
        else // user tabs
        {
            list = viewManager.getGs().getGroup().getUsers().get(index - 1).getUserCalendar().getWeekEvents(current);
        }
        
        calendarComponents.get(index).showEvents(list);
        
        if (tabPanel.getTitleAt(index).equals("All"))
            calendarComponents.get(index).showBusy();
    }
    
    /**
     * Defaults the panel settings
     */
    @Override
    public void clearView()
    {
        // do nothing, nothing to clear
    }

    /**
     * Gets the current date this panel is set to.
     * @return The current date this panel is set to.
     */
    public GregorianCalendar getCurrent()
    {
        return current;
    }

    /**
     * Sets the current date of this panel.
     * @param current The date to set for this panel.
     */
    public void setCurrent(GregorianCalendar current)
    {
        this.current = current;
    }

    /**
     * Gets the tabbed pane in this panel.
     * @return The tabbed pane in this panel.
     */
    public JTabbedPane getTabPanel()
    {
        return tabPanel;
    }
}
